package org.zerg.controller.web;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zerg.dto.Page;
import org.zerg.entity.Manage;
import org.zerg.service.ManageService;


@Controller
@RequestMapping("/manage")
public class ManageWebController {

	@Autowired
	private ManageService manageService;
	
	@Autowired
	private HttpSession session;
	
	@RequestMapping("/list")
	public String getManageList(@RequestParam("page")int page){
		List<Manage> list = manageService.getManage(page);
		Page pa = manageService.pageMessage(page);
		session.setAttribute("list", list);
		session.setAttribute("page", pa);
		return "/manage";
	}
	
	@RequestMapping("/add")
	public String inserManage(Manage manage,@RequestParam("page")int page){
		System.out.println(manage.getName()+"----"+manage.getRole());
		manageService.inserManage(manage);
		return "redirect:/manage/list?page="+page;
	}
	
	@RequestMapping("/update")
	public String updateManage(Manage manage,@RequestParam("page")int page){
		manageService.updateManage(manage);
		return "redirect:/manage/list?page="+page;
	}
	
	@RequestMapping("delete")
	public String deleteManage(@RequestParam("id")int id,@RequestParam("page")int page){
		manageService.deleteManage(id);
		return "redirect:/manage/list?page="+page;
	}
	
	@RequestMapping("isname")
	@ResponseBody
	public boolean isName(@RequestParam("name")String name){
		boolean result = manageService.isName(name);
		return result;
	}
	
	@RequestMapping("isadmin")
	@ResponseBody
	public boolean isAdmin(@RequestParam("admin")String admin){
		boolean result = manageService.isAdmin(admin);
		return result;
	}
}
